---
title: "Shell Script Getting Start"
categories:
- linux
- shell
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org25a569f">数据类型</a>
<ul>
<li><a href="#org7893c61">数组</a>
<ul>
<li><a href="#org09baca4">字符串拆分为数组</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org356770f">运算</a>
<ul>
<li><a href="#org321dacc">使用 {% raw %} $(( ... )) {% endraw %} 方式</a></li>
<li><a href="#org3e7bfb1">expr命令</a></li>
<li><a href="#orgb496070">借助命令完成浮点运算： {% raw %} bc {% endraw %} {% raw %} awk {% endraw %}</a></li>
</ul>
</li>
<li><a href="#orgde67bf8">If</a>
<ul>
<li><a href="#org06592c7">case</a></li>
</ul>
</li>
<li><a href="#org4d13780">循环</a>
<ul>
<li><a href="#org63eed1e">For seq</a></li>
<li><a href="#orge64df09">Bash only</a></li>
<li><a href="#orgad1a702">For Array</a></li>
<li><a href="#org573ff8b">For files</a></li>
<li><a href="#orgb6d907e">While</a></li>
</ul>
</li>
<li><a href="#orgfd2ff86">定义函数</a></li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-org25a569f">
<h2 id="org25a569f">数据类型</h2>
<div class="outline-text-2" id="text-org25a569f">
</div>
<div class="outline-3" id="outline-container-org7893c61">
<h3 id="org7893c61">数组</h3>
<div class="outline-text-3" id="text-org7893c61">
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">以下方式都是bash的特征，unix shell不支持</span>

<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">Define</span>
<span style="font-weight: bold; font-style: italic;">array</span>=(1 2 3 4) <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">元素中间是空格</span>

<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">IndexOf</span>
<span style="font-weight: bold;">echo</span> ${<span style="font-weight: bold; font-style: italic;">array</span>[0]}
${<span style="font-weight: bold; font-style: italic;">arr</span>[*]}         <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">All of the items in the array</span>
${<span style="font-weight: bold; font-style: italic;">!</span>arr[*]}        <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">All of the indexes in the array</span>{% raw %} 
${# {% endraw %}<span style="font-weight: bold; font-style: italic;">arr</span>[*]}        <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">Number of items in the array</span>{% raw %} 
${# {% endraw %}<span style="font-weight: bold; font-style: italic;">arr</span>[0]}        <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">Length of item zero</span>

<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">Sub list</span>
${<span style="font-weight: bold; font-style: italic;">数组名</span>[@或*]:开始下标:结束下标}

<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">Replace Elements</span>
${<span style="font-weight: bold; font-style: italic;">数组名</span>[@或*]/模式/新值}

<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">foreach</span>
<span style="font-weight: bold;">for</span> v<span style="font-weight: bold;"> in</span> ${<span style="font-weight: bold; font-style: italic;">arr_number</span>[@]}; <span style="font-weight: bold;">do</span>
    <span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">v</span>
<span style="font-weight: bold;">done</span>
</pre>
</div>
</div>
<div class="outline-4" id="outline-container-org09baca4">
<h4 id="org09baca4">字符串拆分为数组</h4>
<div class="outline-text-4" id="text-org09baca4">
<blockquote>
<p>
<a href="http://cncc.bingj.com/cache.aspx?q=shell+cut+string+as+array+variable&amp;d=4788231528388522&amp;mkt=zh-CN&amp;setlang=zh-CN&amp;w=xUC8KzhdRwSBb83YWbtoIpld7nPBFXNn">http://cncc.bingj.com/cache.aspx?q=shell+cut+string+as+array+variable&amp;d=4788231528388522&amp;mkt=zh-CN&amp;setlang=zh-CN&amp;w=xUC8KzhdRwSBb83YWbtoIpld7nPBFXNn</a>
</p>
</blockquote>
<div class="org-src-container">
<pre class="src src-bash"><span style="font-weight: bold; font-style: italic;">#</span><span style="font-weight: bold; font-style: italic;">!/bin/</span><span style="font-weight: bold;">bash</span>
<span style="font-weight: bold; font-style: italic;">#</span>
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">Script to split a string based on the delimiter</span>

<span style="font-weight: bold; font-style: italic;">my_string</span>=<span style="font-style: italic;">"Ubuntu;Linux Mint;Debian;Arch;Fedora"</span>  
<span style="font-weight: bold; font-style: italic;">my_array</span>=($(<span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">my_string</span> | tr <span style="font-style: italic;">";"</span> <span style="font-style: italic;">"\n"</span>))

<span style="font-weight: bold; font-style: italic;">#</span><span style="font-weight: bold; font-style: italic;">Print the split string</span>
<span style="font-weight: bold;">for</span> i<span style="font-weight: bold;"> in</span> <span style="font-style: italic;">"${my_array[@]}"</span>
<span style="font-weight: bold;">do</span>
    <span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">i</span>
<span style="font-weight: bold;">done</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org356770f">
<h2 id="org356770f">运算</h2>
<div class="outline-text-2" id="text-org356770f">
</div>
<div class="outline-3" id="outline-container-org321dacc">
<h3 id="org321dacc">使用 {% raw %} $(( ... )) {% endraw %} 方式</h3>
<div class="outline-text-3" id="text-org321dacc">
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;">b</span>=$((5 * 5+5 - 3/2))
<span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">b</span> <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">29</span>

<span style="font-weight: bold; font-style: italic;">i</span>=2
((i++));
<span style="font-weight: bold;">echo</span> <span style="font-style: italic;">"i=$i"</span> <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">i=2</span>
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org3e7bfb1">
<h3 id="org3e7bfb1">expr命令</h3>
<div class="outline-text-3" id="text-org3e7bfb1">
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;">b</span>=$(<span style="font-weight: bold;">expr</span> 5 - 4) <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">important: space is needed</span>
<span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">b</span> <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">1</span>
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-orgb496070">
<h3 id="orgb496070">借助命令完成浮点运算： {% raw %} bc {% endraw %} {% raw %} awk {% endraw %}</h3>
<div class="outline-text-3" id="text-orgb496070">
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;">c</span>=$(<span style="font-weight: bold;">echo</span> <span style="font-style: italic;">"5.01-4*2.0"</span>|bc) 
<span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">c</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;">c</span>=$(<span style="font-weight: bold;">awk</span> <span style="font-style: italic;">'BEGIN{print 7.01*5-4.01 }'</span>)
<span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">c</span>
</pre>
</div>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-orgde67bf8">
<h2 id="orgde67bf8">If</h2>
<div class="outline-text-2" id="text-orgde67bf8">
</div>
<div class="outline-3" id="outline-container-org06592c7">
<h3 id="org06592c7">case</h3>
<div class="outline-text-3" id="text-org06592c7">
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold;">while</span> :
<span style="font-weight: bold;">do</span>
  <span style="font-weight: bold;">read</span> INPUT_STRING
  <span style="font-weight: bold;">case</span> $<span style="font-weight: bold; font-style: italic;">INPUT_STRING</span><span style="font-weight: bold;"> in</span>
        hello)
                <span style="font-weight: bold;">echo</span> <span style="font-style: italic;">"Hello yourself!"</span>
                ;;
        <span style="font-weight: bold;">bye</span>)
                <span style="font-weight: bold;">echo</span> <span style="font-style: italic;">"See you again!"</span>
                <span style="font-weight: bold;">break</span>
                ;;
        *)
                <span style="font-weight: bold;">echo</span> <span style="font-style: italic;">"Sorry, I don't understand"</span>
                ;;
  <span style="font-weight: bold;">esac</span>
<span style="font-weight: bold;">done</span>
</pre>
</div>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org4d13780">
<h2 id="org4d13780">循环</h2>
<div class="outline-text-2" id="text-org4d13780">
</div>
<div class="outline-3" id="outline-container-org63eed1e">
<h3 id="org63eed1e">For seq</h3>
<div class="outline-text-3" id="text-org63eed1e">
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">seq 10 用于生成空格间隔的字符串</span>
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">for spring split with space</span>
<span style="font-weight: bold;">for</span> i<span style="font-weight: bold;"> in</span> $(<span style="font-weight: bold;">seq</span> 10); <span style="font-weight: bold;">do</span>
    <span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">i</span>
<span style="font-weight: bold;">done</span>
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">output: 1 2 3 4 ... 10</span>
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-orge64df09">
<h3 id="orge64df09">Bash only</h3>
<div class="outline-text-3" id="text-orge64df09">
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">下面的表达式不支持sh, 需要bash</span>
<span style="font-weight: bold;">for</span> (( <span style="font-weight: bold; font-style: italic;">i</span>=1; i&lt;=10; i++ ))
<span style="font-weight: bold;">do</span>
    <span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">i</span>
<span style="font-weight: bold;">done</span>
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-orgad1a702">
<h3 id="orgad1a702">For Array</h3>
<div class="outline-text-3" id="text-orgad1a702">
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">for array</span>
<span style="font-weight: bold;">for</span> i<span style="font-weight: bold;"> in</span> {1..10}; <span style="font-weight: bold;">do</span>
    <span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">i</span>
<span style="font-weight: bold;">done</span>
</pre>
</div>
<p>
注意这各方式，在括号内不能再使用变量了，下面的是错误的
</p>
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;">max</span>=10
<span style="font-weight: bold;">for</span> i<span style="font-weight: bold;"> in</span> {1..$<span style="font-weight: bold; font-style: italic;">max</span>}; <span style="font-weight: bold;">do</span>
    <span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">i</span>
<span style="font-weight: bold;">done</span>
</pre>
</div>
<p>
这样输出的结果是 {% raw %} {1..$max} {% endraw %}
</p>
<blockquote>
nil</blockquote>
</div>
</div>
<div class="outline-3" id="outline-container-org573ff8b">
<h3 id="org573ff8b">For files</h3>
<div class="outline-text-3" id="text-org573ff8b">
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold;">for</span> file<span style="font-weight: bold;"> in</span> /usr/local/bin/*;
<span style="font-weight: bold;">do</span>
    <span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">file</span> is file path <span style="font-style: italic;">\!</span> ; <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">file 是文件的全路径</span>
<span style="font-weight: bold;">done</span>
</pre>
</div>
<p>
这种方式能够处理文件名包含空格的情况，而 {% raw %} for file in `ls /usr/local/bin` {% endraw %} 如果目录下有文件名有空格，则会被作为两个。
</p>
</div>
</div>
<div class="outline-3" id="outline-container-orgb6d907e">
<h3 id="orgb6d907e">While</h3>
<div class="outline-text-3" id="text-orgb6d907e">
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;">i</span>=0
<span style="font-weight: bold;">while</span> [ $<span style="font-weight: bold; font-style: italic;">i</span> -lt 4 ]; <span style="font-weight: bold;">do</span>
    <span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">i</span>;
    <span style="font-weight: bold; font-style: italic;">i</span>=<span style="font-weight: bold;">`expr $i + 1`</span>; <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">更多运算表达式查看arithmetic.sh</span>
<span style="font-weight: bold;">done</span>
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">也能够不加条件</span>
<span style="font-weight: bold;">while</span> :
<span style="font-weight: bold;">do</span>
    <span style="font-weight: bold;">echo</span> <span style="font-style: italic;">"doing..."</span>
    <span style="font-weight: bold;">break</span>
<span style="font-weight: bold;">done</span>
</pre>
</div>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-orgfd2ff86">
<h2 id="orgfd2ff86">定义函数</h2>
<div class="outline-text-2" id="text-orgfd2ff86">
<ul class="org-ul">
<li>可以带function fun() 定义，也可以直接fun() 定义,不带任何参数。</li>
<li>参数返回，可以显示加：return 返回，如果不加，将以最后一条命令运行结果，作为返回值。 return后跟数值n(0-255</li>
<li>必须在调用函数地方之前，声明函数，shell脚本是逐行运行。不会像其它语言一样先预编译。一次必须在使用函数前先声明函数。</li>
<li>我们可以将shell中函数，看作是定义一个新的命令，它是命令，因此 各个输入参数直接用 空格分隔。 一次，命令里面获得参数方法可以通过：$0…$n得到。 $0代表函数本身。</li>
<li>定义函数可以与系统命令相同，说明shell搜索命令时候，首先会在当前的shell文件定义好的地方查找，找到直接执行。通过$?获得函数值</li>
<li>如果需要定义自己变量，可以在函数中定义：local 变量=值 ，这时变量就是内部变量，它的修改，不会影响函数外部相同变量的值 。</li>
</ul>
<p>
基本语法：
</p>
<div class="org-src-container">
<pre class="src src-sh">[ <span style="font-weight: bold;">function</span> ] funname [()]
{
    action;
    [return int;]
} 
</pre>
</div>
<p>
例如：
</p>
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold;">function</span> <span style="font-weight: bold;">fSum</span>() <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">parameter list is $1, $2, ...</span>
{
    <span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">1</span>,$<span style="font-weight: bold; font-style: italic;">2</span>;  <span style="font-weight: bold; font-style: italic;">#</span><span style="font-weight: bold; font-style: italic;">　参数</span>
    <span style="font-weight: bold;">return</span> $(($<span style="font-weight: bold; font-style: italic;">1</span>+$<span style="font-weight: bold; font-style: italic;">2</span>));
}
</pre>
</div>
<p>
调用函数就像调用命令一样：
</p>
<div class="org-src-container">
<pre class="src src-sh">fSum 5 7;
<span style="font-weight: bold; font-style: italic;">total</span>=$(<span style="font-weight: bold;">fSum</span> 3 2); <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">调用方法</span>
<span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">total</span>,$<span style="font-weight: bold; font-style: italic;">?</span>;
</pre>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
